package com.java.jiuyang.commons.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.java.jiuyang.commons.entity.Districts;
import com.java.jiuyang.commons.entity.GaoDeEntity;
import com.java.jiuyang.commons.entity.StatusBean;
import com.java.jiuyang.commons.service.GaoDeService;
import com.java.jiuyang.commons.service.GaoDeServiceImpl;
import com.java.jiuyang.commons.utils.HttpClientUtil;
import com.java.jiuyang.commons.utils.JsonUtils;
import org.apache.ibatis.annotations.Param;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * 公共的地图接口：1、传参进来查询数据库是否响应的参数，有就直接返回去
 *              2、数据库查为空，就去高德API去查，查到之后存进数据库里面
 *              3、存进数据库，根据响应的高德返回的字段出入，做一个公共的属性entity对应高德返回的参数。
 *
 * @ClassName: GaoDeCommonsController
 * @Description:
 * @Version: v1.0.0
 * @Author: Fu Hao
 * @Date: 2019/10/24 0024 下午 1:51
 * Modification History:
 * Date           Author      Version     Description
 * -------------------------------------------------------------
 * 2019/10/24 0024        Fu Hao      v1.0.0         创建
 */

@Controller
@RequestMapping("/gao")
public class GaoDeCommonsController {

    private static final Logger log= LoggerFactory.getLogger(GaoDeServiceImpl.class);

    @Autowired
    private GaoDeService service;


    /**
     * 测试调用数据库
     * @return
     */
    @RequestMapping("/map")
    @ResponseBody
    public PageInfo getMapParams(Integer page,Integer size){
        PageInfo pageInfo= service.getMapParams(page,size);
        System.out.println(service.getMapParams(page,size));
        return service.getMapParams(page,size);
    }

    /*行政区域公共接口：根据pid去数据库查询是否有对应的行政区域数据，分页模糊查询*/
    @RequestMapping("/getmap")
    @ResponseBody
    public PageInfo getmap(@Param("page") Integer page,
                           @Param("size") Integer size,
                           @Param("gaoDeEntity") GaoDeEntity gaoDeEntity){
        if (gaoDeEntity.getPid()==null){
            //return log.error("节点不能为空");
            System.out.println("节点不能为空");
        }
        return service.getmap(page,size,gaoDeEntity);

    }


    /*更多地址----》直接去高德API查询*/
    @RequestMapping("/getApi")
    @ResponseBody
    public String getGaoDe(String keyName){
        String keyCode = "87d056f6e8cde7af1fac90480914bafc";
        String admAddress = "https://restapi.amap.com/v3/config/district";
        Map<String, Object> params = new HashMap<>();
        params.put("key", keyCode);
        params.put("keywords", keyName);
        params.put("subdistrict", 1);
        params.put("extensions", "base");
        //或qu查询的地址
        String map=HttpClientUtil.doGet(admAddress, params);
        System.out.println(map);

        return HttpClientUtil.doGet(admAddress, params);

    }

    /**
     * 根据地址获取经纬度
     * @param address
     * @return
     */
    @RequestMapping("/getCenter")
    @ResponseBody
    public String getCenter(String address){
        return service.getCenter(address);
    }

    /**
     * 根据经纬度获取具体地址
     * @param longitude 经度
     * @param latidute 纬度
     * @return
     */
    @RequestMapping("/getAddress")
    @ResponseBody
    public String getAddress(double longitude,double latidute){
        return service.getAddress(longitude,latidute);
    }

    //把字符串提出来
    @RequestMapping("/getlon")
    @ResponseBody
    public String getlon(String center){
        if (center.contains(",")){
            int index=center.indexOf(",");
            //获取经度
            String longitude=center.substring(0,index);
            System.out.println("经度:"+longitude);
            //获取纬度
            String latitude=center.substring(index+1);
            System.out.println("纬度："+latitude);
        }
        return "";
    }


}
